package com.example.hospital.controller;

import com.example.hospital.constant.UserConstants;
import com.example.hospital.dto.DoctorQuery;
import com.example.hospital.dto.LoginForm;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Doctor;
import com.example.hospital.service.DoctorService;
import com.example.hospital.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("doctor")
@Tag(name = "Doctor", description = "医生相关接口")
public class DoctorController {
    @Resource
    private DoctorService doctorService;

    /**
     * 新增医生
     *
     * @param doctor
     * @return doctor id or error
     */
    @Operation(
            summary = "新增医生",
            description = "创建一个新的医生记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回医生id"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addDoctor(@RequestBody Doctor doctor) {
        boolean saved = doctorService.save(doctor);
        return saved ? Result.success("添加成功", doctor.getDoctorId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除医生
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除医生",
            description = "通过医生ID删除指定的医生记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "医生未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteDoctor(@PathVariable("id") Integer id) {
        boolean removed = doctorService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改医生信息
     *
     * @param doctor
     * @return
     */
    @Operation(
            summary = "修改医生信息",
            description = "通过传递新的医生信息更新现有医生",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updateDoctor(@RequestBody Doctor doctor) {
        boolean updated = doctorService.updateById(doctor);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询医生
     *
     * @param id
     * @return doctor or error
     */
    @Operation(
            summary = "根据id查询医生",
            description = "根据医生ID查询医生信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "医生未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Doctor> getDoctor(@PathVariable("id") Integer id) {
        Doctor doctor = doctorService.getById(id);
        return doctor != null ? Result.success("查询成功", doctor) : Result.fail("不存在该医生");
    }

    /**
     * 查询所有医生
     *
     * @return doctor list
     */
    @Operation(
            summary = "查询所有医生",
            description = "获取所有医生记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
            }
    )
    @GetMapping
    public Result<List<Doctor>> listDoctors() {
        return Result.success(doctorService.list());
    }

    /**
     * 医生登录
     *
     * @param loginForm
     * @return
     */
    @Operation(
            summary = "医生登录",
            description = "医生使用手机号和密码登录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "登录成功, 返回token和name"),
                    @ApiResponse(responseCode = "400", description = "登录失败, 参数错误")
            }
    )
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody LoginForm loginForm) {
        return doctorService.login(loginForm);
    }

    /**
     * 获取登录医生信息
     *
     * @return
     */
    @Operation(
            summary = "获取登录医生信息",
            description = "获取当前登录患者的信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "获取成功"),
                    @ApiResponse(responseCode = "401", description = "未登录")
            },
            parameters = {
                    @Parameter(name = "token", description = "登录凭证", in = ParameterIn.HEADER, required = true)
            }
    )
    @GetMapping("/me")
    public Result<Doctor> getLoginDoctor() {
        String phone = UserHolder.getUserPhone();
        UserConstants.Role role = UserHolder.getUserRole();
        if (phone == null || role != UserConstants.Role.DOCTOR) {
            return Result.fail(401, "未登录");
        }
        Doctor doctor = doctorService.getDoctorByPhoneNumber(phone);
        return doctor != null ? Result.success(doctor) : Result.fail("未找到医生");
    }


    /**
     * 根据科室查询医生
     *
     * @param
     * @return
     */
    @Operation(
            summary = "根据科室查询医生",
            description = "根据科室查询医生",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "医生未找到")
            }
    )
    @GetMapping("/of/specialty/{specialty}")
    public Result<List<Doctor>> getDoctorByDepartment(@PathVariable("specialty") String specialty) {
        List<Doctor> doctors = doctorService.getDoctorByDepartment(specialty);
        return doctors != null ? Result.success("查询成功", doctors) : Result.fail("不存在该医生");
    }

    /**
     * 条件查询医生
     *
     * @param doctorQuery
     * @return
     */
    @Operation(
            summary = "条件查询医生",
            description = "根据条件查询医生",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "医生未找到")
            }
    )
    @PostMapping("/query")
    public Result<List<Doctor>> listDoctors(@RequestBody DoctorQuery doctorQuery) {
        List<Doctor> doctors = doctorService.listDoctors(doctorQuery);
        return Result.success(doctors);
    }
}
